package com.yunshipei.ui.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.yunshipei.adapter.DestroyNotifiAdapter;
import com.yunshipei.db.DatabaseUtil;
import com.yunshipei.common.Globals;
import com.yunshipei.enterplorer.R;
import com.yunshipei.manager.YspPreferences;
import com.yunshipei.model.NotifiDeleteModel;
import com.yunshipei.model.NotifyInfo;
import com.yunshipei.ui.activity.NotifiDetailActivity;
import com.yunshipei.ui.dialog.DialogHelper;
import com.yunshipei.ui.dialog.NotifiDestoryDeleteDialog;

import java.util.ArrayList;
import java.util.List;

import io.rong.eventbus.EventBus;

//import de.greenrobot.event.EventBus;

/**
 * A simple {@link Fragment} subclass.
 */
public class DestoryNotifiListFragment extends Fragment implements AdapterView.OnItemClickListener,
        AdapterView.OnItemLongClickListener {

    private ListView listView;
    private List<NotifyInfo> list = new ArrayList<>();
    private DestroyNotifiAdapter destroyNotifiAdapter = null;
    private DatabaseUtil dbu = null;
    private TextView destroyEmptyText = null;
    private String uuid = "";

    public DestoryNotifiListFragment() {
    }


    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.uuid = YspPreferences.getInstance().getSharedPreferences().getString(Globals.UUID, "");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        View view = inflater.inflate(R.layout.fragment_destory_notifi_list, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    private void initView(View view) {
        destroyEmptyText = (TextView) view.findViewById(R.id.destory_empty_content);
        listView = (ListView) view.findViewById(R.id.destory_list);
        listView.setEmptyView(destroyEmptyText);
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
    }

    private void initData() {
        dbu = new DatabaseUtil();
        list = dbu.queryNotifiAll(false, uuid);
        destroyNotifiAdapter = new DestroyNotifiAdapter(getActivity(), list);
        listView.setAdapter(destroyNotifiAdapter);

    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.putExtra(NotifiDetailActivity.FORM, false);
        Bundle bundle = new Bundle();
        bundle.putSerializable("notifiInfo", list.get(position));
        intent.putExtras(bundle);
        intent.setClass(getActivity(), NotifiDetailActivity.class);
        startActivity(intent);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        NotifiDestoryDeleteDialog dialog = DialogHelper.getNotifiDestoryDeleteDialog(getActivity(), list.get(position));
        dialog.show();
        return true;
    }

    public void onEventMainThread(NotifiDeleteModel deleteModel) {
        List<NotifyInfo> list1 = dbu.queryNotifiAll(false, uuid);
        destroyNotifiAdapter = new DestroyNotifiAdapter(getActivity(), list1);
        listView.setAdapter(destroyNotifiAdapter);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
